<template>
	<view class="u-p-30">
		<view class="combox">
			<view class="">
				<view class="u-font-18 f-w">
					身份证正反面
				</view>
				<view class="u-flex u-flex-column u-flex-center u-m-30">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
						:maxCount="1" width="300" height="180" :previewFullImage="false">
						<image src="@/static/images/idfont.png" mode="widthFix"
							style="width: 300px;height: 180px;text-align: center;"></image>
							
					</u-upload>
				</view>
				<view class="u-flex u-flex-column u-flex-center u-m-30">
					<u-upload :fileList="fileList2" @afterRead="afterRead" @delete="deletePic" name="2" multiple
						:maxCount="1" width="300" height="180" :previewFullImage="false">
						<image src="@/static/images/idback.png" 
							style="width: 300px;height: 180px;text-align: center;"></image>
					</u-upload>
				</view>
				<view class="u-font-18 f-w">
					证明照片
				</view>
				<view class="u-flex u-flex-column u-flex-center u-m-t-30">
					<u-upload :fileList="fileList3" @afterRead="afterRead" @delete="deletePic" name="3" multiple
						:maxCount="1" width="300" height="180" :previewFullImage="false">
						<image src="@/static/images/idd.png" 
							style="width: 300px;height: 180px;text-align: center;"></image>
					</u-upload>
				</view>
			</view>
			<view class="combox">
				<u--form labelPosition="left" :model="userInfo" :rules="rules" ref="uForm">
					<u-form-item label="姓名" label-width='70' prop="name" borderBottom ref="item1">
						<u--input v-model="userInfo.name" border="none" placeholder='请输入姓名'></u--input>
					</u-form-item>
					<u-form-item label="身份证号" label-width='70' prop="identity" borderBottom ref="item1">
						<u--input v-model="userInfo.identity" border="none" placeholder='请输入身份证'></u--input>
					</u-form-item>
				</u--form> 
			</view> 
		</view> 
		<view class="u-p-30">
			<u-button @click="submit" type="primary" shape="circle">修改提交</u-button>
		</view> 
	</view>
</template>

<script>
	import IDValidator from '../../../config/IDValidator.js'
	import url from '@/config/url.js';
	export default {
		data() {
			return {
				baseUrl:url.baseUrl,
				fileList1: [],
				fileList2: [],
				fileList3: [],
				userInfo: {
					identity: '',
					idFront: '',
					idBack: '',
					name: '',
				},
				oldData:{},
				rules: {
					'name': {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
					'identity': {
						type: 'number',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
				},
			}
		},
		created() {
			this.getUserInfo()
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			showImage(pic){
				uni.previewImage({
					urls:[pic]
				})
			},
			getUserInfo() {
				this.$api.getUserInfoApi().then((res) => {
					if (res.code == 200) {
						this.userInfo = res.data
						uni.setStorageSync('oldData',res.data)
						this.fileList1 = [{
							url:this.userInfo.idFront
						}]
						this.fileList2 = [{
							url:this.userInfo.idBack
						}]
						this.fileList3 = [{
							url:this.userInfo.shotImg
						}]
					}
				})
			},
			submit() {
				var Validator = new IDValidator();
				if (!Validator.isValid(this.userInfo.identity)) {
					uni.$u.toast('身份证格式不正确')
				} else {
					this.$refs.uForm.validate().then(res => {
						// console.log(this.oldData)
						// console.log(this.oldData.idFront)
						// if(this.userInfo.idFront == undefined){
						// 	this.userInfo.idFront = this.oldData.idFront
						// }
						// if(this.userInfo.idBack== undefined){
						// 	this.userInfo.idBack = this.oldData.idBack
						// }
						// if(this.userInfo.shotImg== undefined){
						// 	this.userInfo.shotImg = this.oldData.shotImg
						// }
						// console.log(this.userInfo.shotImg)
						
						let data = this.userInfo;
						console.log(data)
						console.log('====');
						this.oldData = uni.getStorageSync('oldData')
						console.log(this.oldData)
						this.$api.saveUserInfoApi(data).then((res) => {
							if (res.code == 200) {
								setTimeout(function() {
									uni.$u.route('/pages/user/idCard/idsucc')
								}, 500);
							}
						})
					}).catch(errors => {
						uni.$u.toast('校验失败')
					})
				}
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					let resultArr = JSON.parse(result);
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: resultArr,
						fileName: resultArr.data.ossName,
						fileUrl: resultArr.data.ossUrl,
					}))
					fileListLen++;
					let arr = ['idFront','idBack','shotImg']
					if(event.name == 1){
						this.userInfo.idFront = this[`fileList${event.name}`][0].fileUrl
					}else if(event.name==2){
						this.userInfo.idBack =this[`fileList${event.name}`][0].fileUrl
					}else if(event.name==3){
						this.userInfo.shotImg = this[`fileList${event.name}`][0].fileUrl
					}
					// this.userInfo.arr[i]=this[`fileList${event.name}`][0].fileUrl;
					// console.log(this.userInfo.arr[i])
				}
				// this.userInfo.idFront = this.fileList1[0].fileUrl
				// this.userInfo.idBack = this.fileList2[0].fileUrl
				// this.userInfo.shotImg = this.fileList3[0].fileUrl
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.baseUrl + 'sso/oss/upload', //上传的路径
						filePath: url,
						name: 'file',
						formData: {
							token: uni.getStorageSync('token'),
						},
						header: {
							Authorization: "Bearer " + uni.getStorageSync('token'),
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data)
							}, 1000)
						}
					});
				})
			},
		}

	}
</script>

<style lang="scss" scoped>
 /deep/	.combox .u-upload__wrap{
		justify-content: center !important;
	}
</style>